package com.x.erp.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.x.erp.model.CarLog;
import com.x.erp.util.ConstantState;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.CarSale;
import com.x.erp.rpc.CarSaleRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;

import javax.servlet.http.HttpServletRequest;

/**
 * 销售出库信息
 */
@RestController
public class CarSaleController extends BaseController{

	@Reference
	private CarSaleRpcService carSaleRpcService;//销售出库信息服务

	/**
	 * 查询车辆销售信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getCarSaleList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getCarSaleList(CarSale carSale) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carSaleRpcService.selectCarSaleList(carSale));
	}

	/**
	 * 查询车辆销售数量
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getCarSaleNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getCarSaleNum(CarSale carSale) {
		Map<String, Object> res = new HashMap<>();
		res.put("total", carSaleRpcService.selectCarSaleNum(carSale));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 根据ID查询销售出库信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getCarSaleById", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getCarSaleById(CarSale carSale) {
		if(carSale == null || StringUtils.isEmpty(carSale.getSaleId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "销售出库ID不能为空！");
		}
		List<CarSale> carSales = carSaleRpcService.selectCarSaleList(carSale);
		if(carSales != null && carSales.size() > 0) {
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功！", carSales.get(0));
		}
		return new ResultModel(ResultCode.RESULT_FAIL, "查询失败");
	}

	/**
	 * 新增销售出库信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertCarSale", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertCarSale(CarSale carSale, HttpServletRequest request) {
		String source = request.getHeader("source");
		if(carSale == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "销售出库信息不能为空！");
		}
		if(StringUtils.isEmpty(carSale.getCarId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆ID不能为空！");
		}
		if(StringUtils.isEmpty(carSale.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "操作用户ID不能为空！");
		}
		if(carSale.getSaleType() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "销售类型不能为空！");
		}
		if(carSale.getSaleDealDate() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "成交日期不能为空！");
		}
		if(carSale.getSalePrice() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "成交价格不能为空！");
		}
		if(StringUtils.isEmpty(carSale.getSaleDealUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "成交员工ID不能为空！");
		}
		//记录日志信息
		CarLog carLog = new CarLog("销售出库", new Date(), carSale.getUserId(), carSale.getCarId());
		carLog.setCarStateKey("carStockState");
		carLog.setCarStateValue(ConstantState.CAR_STOCK_STATE_OUT_FINISH);
		carLog.setCarLogSource(source);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！", carSaleRpcService.insertCarSale(carSale, carLog));
	}

	/**
	 * 修改销售出库信息
	 * @param carSale
	 * @return
	 */
	@RequestMapping(value = "/updateCarSale", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel updateCarSale(CarSale carSale) {
		if(carSale == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "销售出库信息不能为空！");
		}
		if(StringUtils.isEmpty(carSale.getSaleId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "销售出库ID不能为空！");
		}
		carSaleRpcService.updateCarSale(carSale);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！");
	}

}
